var a = getApp()
Page({
  data: {
    xyshow: 1,
    imageList: [],
    sub_btn: 1,
    pic_img: '',
    id_pic: ''
  },
  onLoad: function (options) {
    getApp().pageOnLoad(this, options)
    this.getInfo()
  },

  getInfo: function () {
    var that = this;
    a.request({
        url: getApp().api.distributor.get_info,
        method:'POST',
        success:function (e) {
          if (e.data.dis && e.data.dis.status == 1) {
            wx.redirectTo({
              url: '/distributor/index/index',
            })
          }else {
            that.setData({
              dis: e.data.dis,
              entry_rules: e.data.option.entry_rules,
              pic_img: e.data.dis ? e.data.dis.id_pic : ''
            })
          }
        },
        fail:function () {
            
        },
        complete: function () {
          wx.hideLoading();
        }
    })
  },
  onShow: function () {
    getApp().pageOnShow(this);
  },
  onShareAppMessage: function () {

  },
  formSubmitMessage: function (e) {
    var that = this;
    if (typeof wx.requestSubscribeMessage === "function") {
      var shop_is_tpl = that.data.shop_is_tpl;
      return wx.requestSubscribeMessage({
        tmplIds: [shop_is_tpl],
        success: function (res) {
          if (shop_is_tpl != '' && res[shop_is_tpl] == "accept") {
            that.data.form_id = shop_is_tpl
          }
        },
        fail: function (res) {

        },
        complete: function (res) {
          that.read(e);
        }
      })
    } else {
      that.read(e);
    }
  },
  chooseImage: function () {
    var that = this
    wx.chooseImage({
        sourceType: ['album', 'camera'],
        count: 1,
        success: function (res) {
            that.setData({
                imageList: res.tempFilePaths,
                shoose:true
            })
            wx.showLoading({
                title: '提交中',
            });
            wx.uploadFile({
              url: getApp().api.default.upload_image,
              filePath: res.tempFilePaths[0],
              name: 'image',
              formData: {},
              success: function (res) {
                  res = JSON.parse(res.data);
                  if(res.code>0){
                      wx.showModal({
                          title:'提示',
                          content: res.msg,
                          showCancel:false,
                      })
                  }else {
                    that.setData(
                      {
                        pic_img: res.data.url
                      }
                    )
                  }
              },
              complete: function () {
                wx.hideLoading();
              }
          })
        }
    })
  },
  read:function() {
    var check = this.data.check;
    if (check) {
        this.setData({
            check:false,
            sub_btn: true
        })
    } else {
        this.setData({
            check: true,
            sub_btn: false 
        })
    }
  },
  hyxzboxshow: function (e) {
    this.setData({
        xyshow: false
    })
  },
  hyxzboxhide: function (e) {
    this.setData({
        xyshow: true,
        check:true,
        sub_btn: false
    })
  },
  formSubmit:function (e) {
      var that = this;
      let {real_name,mobile,id_code} = e.detail.value;
      if (!that.data.pic_img) {
        wx.showModal({
          title: '提示',
          content: '请上传身份证照片',
          showCancel: false,
        })
        return false;
      }
      wx.showLoading({
          title: '提交中',
      });

      a.request({
        url: getApp().api.distributor.submit,
        method:'POST',
        data:{
            mobile:mobile,
            id_code: id_code,
            real_name:real_name,
            id_pic:that.data.pic_img,
        },
        success:function (e) {
            if(e.code>0){
                wx.showModal({
                    title:'提示',
                    content: e.msg,
                    showCancel:false,
                })
            }
            if(e.code==0){
                wx.showModal({
                    title:'提示',
                    content: e.msg,
                    showCancel:false,
                    success:function (e) {
                      wx.switchTab({    //保留当前页面，跳转到应用内的某个页面（最多打开5个页面，之后按钮就没有响应的）
                        url: "/pages/index/index"
                      })
                    }
                })
            }

        },
        fail:function () {
            wx.showModal({
                title:'提示',
                content: '提交失败',
                showCancel:false,
            })
        },
        complete: function () {
          wx.hideLoading();
        }
    })

  },
})